'use strict'

const token = require('../token')

const tok = new token({
  urlencoded : true,
  //alg: 'aes-192-cbc'
})

//tok.setExpires(1)

tok.setEncoding('base64url')

let tidlist = ['t1','t2','t3','t4']

tok.addTokenId(tidlist)

tok.setMapId('tit')

let info = {
  id : 1001,
  //name : 'brave'
}
console.log('加密处理···')
let edata = tok.make(info)

console.log('解密处理···')
let realData = tok.verify(edata)

console.log(edata)
console.log(realData)

let edataList = []

console.log('测试随机tokenId加密')
for (let i = 0 ; i < 10; i++) {
  edata = tok.make(info)
  edataList.push(edata)
}

if ( parseInt(Math.random() * 10) > 5 ) {
  tok.removeTokenId('t2')
  tok.removeTokenId('t3')
}

console.log('测试随机tokenId解密')
for (let i = 0; i < 10; i++) {
  realData = tok.verify(edataList[i])
  console.log(realData)
}

edata = tok.make(info, 'tit')

console.log(tok.verify(edata))

let total = 100000

tok.setIdKeyIv('xyz','qazxswedcfvrgthynujmkiolp0987654', 'qawsedrftgyhujik')
tok.setIdKeyIv('io', 'qazxswedcfvrgthynujmkiolp0e24565', 'qawsedrftgyhujik')
tok.setIdKeyIv('cd', 'qazxswedcfvrgthynujmkiolpr5tf765', 'qawsedrftgyhujie')
tok.setIdKeyIv('mk', 'qazxswedcfvrgthynujmkiolpw347f65', 'qawsedrftgyhujir')

let ikv = {
  id : 'io',
  key : tok.fixKey('qazxswedcfvrgthynujmkiolp0e24565'),
  iv : 'qawsedrftgyhujik'
}
edata = tok.makeikv(info, ikv)

realData = tok.verifyikv(edata, ikv)

console.log('rand iv key token', tok.randIvToken(info))

console.log('--IKV-TEST--:', edata, realData)

console.log('测试指定key和iv', total, '次加解密性能')

let st = Date.now()

for (let i = 0; i < total; i++) {
  //edata = tok.make(info, 'xyz')
  //realData = tok.verify(edata, 'qazxswedcfvrgthynujmkiolp0987654', 'qawsedrftgyhujik', 'xyz')
  //realData = tok.verifyId(edata, 'xyz')
  
  //edata = tok.make(info, 'io')
  //realData = tok.verifyid(edata, 'io')
  //console.log(edata, realData)
  info.expires = i % 7 ? i : '122133sdaf'
  edata = tok.randIvToken(info)
  realData = tok.verifyikv(edata.token, edata)
  //console.log(edata, realData)
}

let et = Date.now()

console.log(et - st, 'ms')

console.log(tok.randIvToken(info))
